﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Home.Mobilesales.Common
{
    [Serializable]
    public class Settings
    {
        //Details
        private string _companyName = string.Empty;
        private string _agentName = string.Empty;

        //DataBase
        private string _databasePath = string.Empty;
        private string _databasePassword = string.Empty;

        //GPS
        private bool _enableGPS = false;
        private string _portGPS = string.Empty;

        //ImportExportData
        private string _importPath = string.Empty;
        private string _exportPath = string.Empty;
        private bool _isSaveFiles = false;

        //RemainingGoods
        private bool _checkRemainingGoods = true;
        private bool _visibleNullGoods = false;
        private bool _isVan = false;

        //Ftp
        private string _ftpUrl;
        private int _ftpPort = 21;
        private string _ftpLogin;
        private string _ftpPassword;
        private bool _isPassivMode;
        private string _ftpImportPath = string.Empty;
        private string _ftpExportPath = string.Empty;

        //Document 
        private int _defaultSklad = 1;
        private bool _autoGenerateNumber = false;
        private int _number = 1;

        //Print
        private string _printPath = string.Empty;
        private string _executeProgramPrint = string.Empty;

        
        [NonSerialized]
        protected static Settings _instance = null;

        public string CompanyName
        {
            get 
            { 
                return _companyName;
            }
            set
            {
                _companyName = value;
            }
        }
       
        public string AgentName
        {
            get
            {
                return _agentName;
            }
            set
            {
                _agentName = value;
            }
        }

        public string DatabasePath 
        {
            get
            {
                return _instance._databasePath;
            }
            set
            {
                _instance._databasePath = value;
            } 
        }

        public string DatabasePassword
        {
            get
            {
                return _instance._databasePassword;
            }
            set
            {
                _instance._databasePassword = value;
            }
        }

        public bool EnableGPS
        {
            get
            {
                return _instance._enableGPS;
            }
            set
            {
                _instance._enableGPS = value;
            }
        }

        public string PortGPS
        {
            get
            {
                return _instance._portGPS;
            }
            set
            {
                _instance._portGPS = value;
            }
        }
        
        public string ImportPath
        {
            get
            {
                return _importPath;
            }
            set
            {
                _importPath = value;
            }
        }

        public string ExportPath
        {
            get
            {
                return _exportPath;
            }
            set
            {
                _exportPath = value;
            }
        }

        public bool IsSaveFiles
        {
            get 
            {
                return _isSaveFiles;
            }
            set
            {
                _isSaveFiles = value;
            }
        }

        public bool CheckRemainingGoods
        {
            get
            {
                return _checkRemainingGoods;
            }
            set
            {
                _checkRemainingGoods = value;
            }
        }

        public bool VisibleNullGoods
        {
            get 
            {
                return _visibleNullGoods;
            }
            set
            {
                _visibleNullGoods = value;
            }
        }

        public bool IsVanSales
        {
            get
            {
                return _isVan;
            }
            set
            {
                _isVan = value;
            }
        }
        
        public string FtpUrl
        {
            get { return _ftpUrl; }
            set { _ftpUrl = value; }
        }

        public int FtpPort
        {
            get { return _ftpPort; }
            set { _ftpPort = value; }
        }

        public string FtpLogin
        {
            get
            {
                return _ftpLogin;
            }
            set { _ftpLogin = value; }
        }
       
        public string FtpPassword
        {
            get { return _ftpPassword; }
            set { _ftpPassword = value; }
        }
       
        public bool IsPassivMode
        {
            get { return _isPassivMode; }
            set { _isPassivMode = value; }
        }

        public string FtpImportPath
        {
            get { return _ftpImportPath; }
            set { _ftpImportPath = value;}
        }
        
        public string FtpExportPath
        {
            get { return _ftpExportPath; }
            set { _ftpExportPath = value; }
        }

        public int DefaultSklad
        {
            get { return _defaultSklad; }
            set { _defaultSklad = value; }
        }

        public bool AutoGenerateNumber
        {
            get { return _autoGenerateNumber; }
            set { _autoGenerateNumber = value; }
        }
        
        public int NumberDocument
        {
            get { return _number; }
            set { _number = value; }
        }

        public string PrintPath
        {
            get { return _printPath; }
            set { _printPath = value; }
        }
        public string ExecuteProgramPrint
        {
            get { return _executeProgramPrint; }
            set { _executeProgramPrint = value; }
        }


        public Settings()
        {
            _instance = this;
        }

        public static Settings GetInstance ()
        {
            if (_instance == null)
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof (Settings));
                    FileStream fstream =
                        new FileStream(
                            Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Settings.xml",
                            FileMode.Open);
                    try
                    {
                        _instance = (Settings) serializer.Deserialize(fstream);
                    }
                    finally
                    {
                        fstream.Close();
                    }
                }
                catch(Exception)
                {
                    _instance = new Settings();
                    throw new Exception("Помилка при завантаженні настройок !");
                }
            }
            return _instance;
        }

        public void Save ()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            FileStream fstream =
                new FileStream(
                    Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Settings.xml",
                    FileMode.Create);
            try
            {
                serializer.Serialize(fstream, _instance);
            }
            finally
            {
                fstream.Close();
            }
        }
    }
}
